#ifndef XPLAYER_AV_PLAYER_CORE_H
#define XPLAYER_AV_PLAYER_CORE_H

#include <cstdint>
#include <string>
#include <queue>
#include <vector>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <android/native_window.h>
#include "decoder_controller_api.h"
#include "render_controller_api.h"
#include "filter_controller_api.h"
#include "media_common.h"
#include "av_render_callback.h"
#include "av_player_common.h"
extern "C" {
#include "libavutil/frame.h"
}

// 状态回调函数类型定义
class AVPlayerCore;
typedef void (*StateCallback)(MediaState, AVPlayerCore*);

class AVPlayerCore {
public:
    AVPlayerCore();
    ~AVPlayerCore();

    // 禁止拷贝和移动
    AVPlayerCore(const AVPlayerCore&) = delete;
    AVPlayerCore& operator=(const AVPlayerCore&) = delete;

    // 核心控制接口（线程安全）
    void initialize(const std::string& url, ANativeWindow* window);
    void startPlayback();
    void pausePlayback();
    void stopPlayback();
    void seekTo(int64_t us);

    // 状态查询接口
    MediaState getState() const { return state_.load(std::memory_order_acquire); }
    const McMediaInfo* getMediaInfo() const { return media_info_; }
    int64_t getCurrentPosition() {
        if (!is_seeking_) {
            current_pts_ = render_callback_.getCurrentPts();
        }
        return current_pts_;
    }

    // 回调设置
    void setStatusCallback(StateCallback callback);

private:
    // 线程主函数
    void workerMain();

    // 初始化流程
    bool initComponents();
    bool initDecoder();
    bool initFilter();
    bool initRenderer();

    // 播放主循环
    void playbackLoop();
    bool decodeFrames();
    bool filterFrames();
    bool renderFrames();

    // 资源管理
    void clearFrameQueues();
    void releaseComponents();

    // 状态管理
    void updateState(MediaState new_state);

    // 核心组件
    McDecoderAPI* decoder_ = nullptr;
    McFilterAPI* filter_ = nullptr;
    McRenderAPI* renderer_ = nullptr;
    RenderCallback render_callback_ = {};

    // 帧缓存队列
    std::vector<std::pair<McMediaStreamType, std::queue<AVFrame*>>> frames_decode_;
    std::vector<std::pair<McMediaStreamType, std::queue<AVFrame*>>> frames_filter_;
    std::vector<bool> stream_eof_;
    int active_decode_streams_ = 0;

    // 配置参数
    std::string media_url_;
    ANativeWindow* native_window_ = nullptr;
    const McMediaInfo *media_info_ = nullptr;
    int buffer_size_render_[2];

    // 配置文件
    McDecodeConfig decoder_config_ = {};

    McFilterConfig filter_config_ = {};
    McFilterConfigAudio filter_config_audio_ = {};
    McFilterConfigVideo filter_config_video_ = {};

    McRenderConfig render_config_ = {};
    McRenderConfigAudio render_config_audio_ = {};
    McRenderConfigVideo render_config_video_ = {};

    // 状态变量（原子类型保证线程安全）
    std::atomic<MediaState> state_{MediaState::IDLE};
    std::atomic<bool> is_seeking_{false};
    std::atomic<int64_t> seek_target_us_{0};

    // 线程与同步
    std::thread worker_thread_;
    mutable std::mutex core_mutex_;  // 唯一的核心锁
    std::condition_variable core_cv_; // 唯一的条件变量

    // 回调相关
    StateCallback state_callback_ = nullptr;
    int64_t current_pts_ = 0;

    // 视频硬解直通
    bool is_through_ = false;
};

#endif // XPLAYER_AV_PLAYER_CORE_H